package com.itbour.web.action.trade;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.itbour.com.page.JspPage;
import com.itbour.com.page.Page;
import com.itbour.common.base.BaseAction;
import com.itbour.common.util.StringUtils;
import com.itbour.dto.com.UserInfoDto;
import com.itbour.dto.trade.ActivityInfoDto;
import com.itbour.dto.trade.ActivityRInfoDto;
import com.itbour.dto.trade.PriceInfoDto;
import com.itbour.dto.trade.PrintRelInfoDto;
import com.itbour.service.trade.ActivityInfoService;
import com.opensymphony.xwork2.ActionContext;

/**
 * 活动场次	Action
 * @author 	LQ 李琦
 *
 */
public class GetActivityInfoAction extends BaseAction {

	/**
	 */
	private static final long serialVersionUID = 1L;
	private ActivityInfoService activityInfoService;
	private Map<String, Object> mapResult=new HashMap<String, Object>();
	private ActivityInfoDto activityInfoDto;//活动实体
	private ActivityRInfoDto activityRInfoDto;//活动商品实体
	
	private List<ActivityInfoDto> listResult;//活动
	private List<ActivityRInfoDto> listARInfo;//单个活动商品
	private List<PrintRelInfoDto> combo;//套餐
	private List<PriceInfoDto> ppPrice;//套餐价格计量
	private String findType;
	private String findState;
	private String findPu;
	private String findTitle;
	
	private String findItemNo;// 检索条件[商品编号]
	private String findItemName;// 检索条件[商品名称]
	private String findItemTypeL;// 检索条件[商品大分类]
	private String findItemTypeM;// 检索条件[商品中分类]
	private String findItemType;// 检索条件[商品分类]
	private String findItemState;// 检索条件[商品状态]
	private String findDesignId;// 检索条件[关联模版id]
	
	
	/**	获取活动场次信息
	 *  @return
	 */
	public String getActivityInfo(){
		Map<String, Object> session=ActionContext.getContext().getSession();
		UserInfoDto user=(UserInfoDto)session.get("user");
		if(user!=null && user.getUser_id()!=null){
			HttpServletRequest request = ServletActionContext.getRequest();
			Integer pageNo = Integer.parseInt(request.getParameter("page"));
			Integer pageSize = Integer.parseInt(request.getParameter("rows"));
			Map<String, Object> parm=new HashMap<String, Object>();
			if(StringUtils.isNotEmpty(findType)){
				parm.put("type",findType);
			}
			if(StringUtils.isNotEmpty(findState)){
				parm.put("state",findState);
			}
			if(StringUtils.isNotEmpty(findPu)){
				parm.put("publish",findPu);
			}
			if(StringUtils.isNotEmpty(findTitle)){
				parm.put("title",findTitle);
			}
			Page p=activityInfoService.getActivityInfo(parm, pageNo, pageSize);
			mapResult.put("total",p.getTotalCount());
			mapResult.put("rows",p.getResult());
		}
		return SUCCESS;
	}
	
	/**	获取活动商品*/
	public String getActItemInfo(){
		Map<String, Object> session=ActionContext.getContext().getSession();
		UserInfoDto user=(UserInfoDto)session.get("user");
		if(user!=null && user.getUser_id()!=null){
			HttpServletRequest request = ServletActionContext.getRequest();
			Integer actId = Integer.parseInt(request.getParameter("actId"));
			Integer pageNo = Integer.parseInt(request.getParameter("page"));
			Integer pageSize = Integer.parseInt(request.getParameter("rows"));
			Map<String, Object> parm=new HashMap<String, Object>();
			if(StringUtils.isNotEmpty(findType)){
				parm.put("type",findType);
			}
			if(StringUtils.isNotEmpty(findState)){
				parm.put("state",findState);
			}
			if(StringUtils.isNotEmpty(findPu)){
				parm.put("publish",findPu);
			}
			if(StringUtils.isNotEmpty(findTitle)){
				parm.put("title",findTitle);
			}
			if(actId!=null){
				parm.put("act_id",actId);
				Page p=activityInfoService.getActItemInfo(parm, pageNo, pageSize);
				mapResult.put("total",p.getTotalCount());
				mapResult.put("rows",p.getResult());
			}
		}
		return SUCCESS;
	}

	// 分页获取商品信息
	public String getItemsInfoPg() throws Exception {
		// 用户信息
		Map<String, Object> session = ActionContext.getContext().getSession();
    	UserInfoDto user = (UserInfoDto)session.get("user");
    	if(user!=null){
			HttpServletRequest request = ServletActionContext.getRequest();
			String pageNumber = request.getParameter("page");
			String pageSize = request.getParameter("rows");
			Map<String, Object> param = new HashMap<String, Object>();
			if (StringUtils.isNotEmpty(findItemNo)) {
				param.put("item_no", "%"+findItemNo+"%");
			}
			if (StringUtils.isNotEmpty(findItemName)) {
				param.put("item_name", "%"+findItemName+"%");
			}
			if (StringUtils.isNotEmpty(findItemTypeL)) {
				param.put("type_id_l", findItemTypeL);
			}
			if (StringUtils.isNotEmpty(findItemTypeM)) {
				param.put("type_id_m", findItemTypeM);
			}
			if (StringUtils.isNotEmpty(findItemType)) {
				param.put("type_id", findItemType);
			}
			if (StringUtils.isNotEmpty(findItemState)) {
				param.put("state", findItemState);
			}
			if (StringUtils.isNotEmpty(findDesignId)) {
				param.put("design_id", findDesignId);
			}
			Page page = activityInfoService.itemsPgQuery(param,
					Integer.parseInt(pageNumber),
					Integer.parseInt(pageSize));
			mapResult.put("total", page.getTotalCount());
			mapResult.put("rows", page.getResult());
    	}
		return SUCCESS;
	}
	
	//获取单个商品套餐信息
	public String getItemComboInfo(){
		Map<String, Object> session=ActionContext.getContext().getSession();
		UserInfoDto user=(UserInfoDto)session.get("user");
		if(user!=null && user.getUser_id()!=null){
			Map<String, Object> parm=new HashMap<String, Object>();
			HttpServletRequest request = ServletActionContext.getRequest();
			Integer itemId = Integer.parseInt(request.getParameter("itemId"));
			if(itemId!=null){
				parm.put("item_id",itemId);
			}
			combo=activityInfoService.getItemComboInfo(parm);
			Page p = new JspPage(0, 0, 10, combo);
			mapResult.put("total", p.getTotalCount());
			mapResult.put("rows", p.getResult());
		}
		return SUCCESS;
	}
	
	//获取套餐数量价格表 itb_qty_price 套餐价格计量信息
	public String getQtyPriceInfo(){
		Map<String, Object> session=ActionContext.getContext().getSession();
		UserInfoDto user=(UserInfoDto)session.get("user");
		if(user!=null && user.getUser_id()!=null){
			Map<String, Object> parm=new HashMap<String, Object>();
			HttpServletRequest request = ServletActionContext.getRequest();
			Integer ppId = Integer.parseInt(request.getParameter("ppId"));
			if(ppId!=null){
				parm.put("pp_id",ppId);
			}
			ppPrice=activityInfoService.getQtyPriceInfo(parm);
			Page p = new JspPage(0, 0, 10, ppPrice);
			mapResult.put("total", p.getTotalCount());
			mapResult.put("rows", p.getResult());
		}
		return SUCCESS;
	}
	
	/**添加
	 * 活动 */
	public void addActivityInfo() throws Exception{
		Map<String, Object> session=ActionContext.getContext().getSession();
		UserInfoDto user=(UserInfoDto)session.get("user");
		if(user!=null && user.getUser_id()!=null){
			activityInfoDto.setUpdate_by(user.getUser_id().toString());
			activityInfoService.insertActivityInfo(activityInfoDto);
			this.writeJSON("ok");
		}else{
			this.writeJSON("no");
		}
	}
	/**添加
	 * 活动商品*/
	public void addActivityItemInfo() throws Exception{
		Map<String, Object> session=ActionContext.getContext().getSession();
		UserInfoDto user=(UserInfoDto)session.get("user");
		if(user!=null && user.getUser_id()!=null&&activityRInfoDto!=null){
			System.out.println(user.getUser_id());
			String upBy = String.valueOf(user.getUser_id());
			activityRInfoDto.setUpdate_by(upBy);
			activityInfoService.insertActivityItemInfo(activityRInfoDto);
			this.writeJSON("ok");
		}else{
			this.writeJSON("no");
		}
	}
	
	/** * 修改活动信息*/
	public void updActivityInfo() throws Exception{
		Map<String, Object> session=ActionContext.getContext().getSession();
		UserInfoDto user=(UserInfoDto)session.get("user");
		if(user!=null && user.getUser_id()!=null&&activityInfoDto.getAct_id()!=null){
			activityInfoDto.setUpdate_by(user.getUser_id().toString());
			activityInfoService.updateActivityInfo(activityInfoDto);
			this.writeJSON("ok");
		}else{
			this.writeJSON("no");
		}
	}
	/** * 修改活动商品*/
	public void updActivityRInfo() throws Exception{
		Map<String, Object> session=ActionContext.getContext().getSession();
		UserInfoDto user=(UserInfoDto)session.get("user");
		if(user!=null && user.getUser_id()!=null&&activityRInfoDto.getR_id()!=null){
			activityRInfoDto.setUpdate_by(user.getUser_id().toString());
			activityInfoService.updateActivityRInfo(activityRInfoDto);
			this.writeJSON("ok");
		}else{
			this.writeJSON("no");
		}
	}
	
	/**获取单个 活动*/
	public String getOneActivityInfo(){
		Map<String, Object> session=ActionContext.getContext().getSession();
		UserInfoDto user=(UserInfoDto)session.get("user");
		if(user!=null && user.getUser_id()!=null){
			HttpServletRequest request=ServletActionContext.getRequest();
			Integer actId = Integer.parseInt(request.getParameter("actId"));
			Map<String, Object> parm=new HashMap<String, Object>();
			parm.put("act_id",actId);
			listResult=activityInfoService.getOneActivityInfo(parm);
		}
		return SUCCESS;
	}
	
	/**获取单个 活动商品信息*/
	public String getOneActivityRInfo(){
		Map<String, Object> session=ActionContext.getContext().getSession();
		UserInfoDto user=(UserInfoDto)session.get("user");
		if(user!=null && user.getUser_id()!=null){
			HttpServletRequest request=ServletActionContext.getRequest();
			Integer rId = Integer.parseInt(request.getParameter("rId"));
			Map<String, Object> parm=new HashMap<String, Object>();
			parm.put("r_id",rId);
			listARInfo=activityInfoService.getOneActivityRInfo(parm);
		}
		return SUCCESS;
	}
	
	/**删除活动场次*/
	public void delActivityInfo() throws Exception{
		Map<String, Object> session=ActionContext.getContext().getSession();
		UserInfoDto user=(UserInfoDto)session.get("user");
		if(user!=null && user.getUser_id()!=null){
			HttpServletRequest request=ServletActionContext.getRequest();
			String ids = request.getParameter("ids");
			if(ids!=null){
				Map<String, Object> parm=new HashMap<String, Object>();
				parm.put("ids",ids);
				activityInfoService.delActivityInfo(parm);
				this.writeJSON("ok");
			}else{
				this.writeJSON("no");
			}
		}
	}
	
	/**删除活动商品*/
	public void delActivityRInfo() throws Exception{
		Map<String, Object> session=ActionContext.getContext().getSession();
		UserInfoDto user=(UserInfoDto)session.get("user");
		if(user!=null && user.getUser_id()!=null){
			HttpServletRequest request=ServletActionContext.getRequest();
			String ids = request.getParameter("ids");
			if(ids!=null){
				Map<String, Object> parm=new HashMap<String, Object>();
				parm.put("ids",ids);
				activityInfoService.delActivityRInfo(parm);
				this.writeJSON("ok");
			}else{
				this.writeJSON("no");
			}
		}
	}
	
	/**发布活动
	 * @throws Exception 
	 */
	public void publishActInfo() throws Exception{
		Map<String, Object> session=ActionContext.getContext().getSession();
		UserInfoDto user=(UserInfoDto)session.get("user");
		if(user!=null && user.getUser_id()!=null){
			HttpServletRequest request=ServletActionContext.getRequest();
			String ids = request.getParameter("ids");
			Integer pu = Integer.parseInt(request.getParameter("pu"));//设置状态 1发布    0 未发布
			if(ids!=null){
				Map<String, Object> parm=new HashMap<String, Object>();
				parm.put("ids",ids);
				parm.put("publish",pu);
				activityInfoService.publishActInfo(parm);
				this.writeJSON("ok");
			}else{
				this.writeJSON("no");
			}
		}
	}
	
	
	
	public String getFindType() {
		return findType;
	}
	public void setFindType(String findType) {
		this.findType = findType;
	}
	public String getFindState() {
		return findState;
	}
	public void setFindState(String findState) {
		this.findState = findState;
	}
	public String getFindPu() {
		return findPu;
	}
	public void setFindPu(String findPu) {
		this.findPu = findPu;
	}
	public String getFindTitle() {
		return findTitle;
	}
	public void setFindTitle(String findTitle) {
		this.findTitle = findTitle;
	}
	public List<ActivityInfoDto> getListResult() {
		return listResult;
	}
	public void setListResult(List<ActivityInfoDto> listResult) {
		this.listResult = listResult;
	}
	public ActivityInfoService getActivityInfoService() {
		return activityInfoService;
	}
	public void setActivityInfoService(ActivityInfoService activityInfoService) {
		this.activityInfoService = activityInfoService;
	}
	public static long getSerialversionuid() {
		return serialVersionUID;
	}
	public Map<String, Object> getMapResult() {
		return mapResult;
	}
	public void setMapResult(Map<String, Object> mapResult) {
		this.mapResult = mapResult;
	}
	public ActivityInfoDto getActivityInfoDto() {
		return activityInfoDto;
	}
	public void setActivityInfoDto(ActivityInfoDto activityInfoDto) {
		this.activityInfoDto = activityInfoDto;
	}

	public String getFindItemNo() {
		return findItemNo;
	}

	public void setFindItemNo(String findItemNo) {
		this.findItemNo = findItemNo;
	}

	public String getFindItemName() {
		return findItemName;
	}

	public void setFindItemName(String findItemName) {
		this.findItemName = findItemName;
	}

	public String getFindItemTypeL() {
		return findItemTypeL;
	}

	public void setFindItemTypeL(String findItemTypeL) {
		this.findItemTypeL = findItemTypeL;
	}

	public String getFindItemTypeM() {
		return findItemTypeM;
	}

	public void setFindItemTypeM(String findItemTypeM) {
		this.findItemTypeM = findItemTypeM;
	}

	public String getFindItemType() {
		return findItemType;
	}

	public void setFindItemType(String findItemType) {
		this.findItemType = findItemType;
	}
	public String getFindItemState() {
		return findItemState;
	}
	public void setFindItemState(String findItemState) {
		this.findItemState = findItemState;
	}
	public String getFindDesignId() {
		return findDesignId;
	}
	public void setFindDesignId(String findDesignId) {
		this.findDesignId = findDesignId;
	}

	public List<PrintRelInfoDto> getCombo() {
		return combo;
	}
	public void setCombo(List<PrintRelInfoDto> combo) {
		this.combo = combo;
	}
	public List<PriceInfoDto> getPpPrice() {
		return ppPrice;
	}
	public void setPpPrice(List<PriceInfoDto> ppPrice) {
		this.ppPrice = ppPrice;
	}
	public ActivityRInfoDto getActivityRInfoDto() {
		return activityRInfoDto;
	}
	public void setActivityRInfoDto(ActivityRInfoDto activityRInfoDto) {
		this.activityRInfoDto = activityRInfoDto;
	}
	public List<ActivityRInfoDto> getListARInfo() {
		return listARInfo;
	}
	public void setListARInfo(List<ActivityRInfoDto> listARInfo) {
		this.listARInfo = listARInfo;
	}
}
